package net.easyjava.test.changxiba;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.easyjava.third.db.mysql.MysqlClient;
import net.easyjava.tools.http.HttpTools;

import java.io.IOException;
import java.sql.SQLException;

/**
 * Created by wxnacy on 16/6/24.
 */
public class GetData {
    public static final String SONG_LIST_URL = "http://cctv.wookong.cn/get.mvc/getAccompanyList?cid=0&isrecommend=0&pageno=0&pagesize=10000";
    public static final String  jdbcUrl = "jdbc:mysql://mingjuer.mysql.rds.aliyuncs.com:3306/crawler";
    public static final String  jdbcName = "juer";
    public static final String  jdbcPassword = "Juer2016";



    public static void save(){
        MysqlClient client = null;
        try {
            client = MysqlClient.getInstance(jdbcUrl, jdbcName, jdbcPassword);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        try {
            String data = HttpTools.get(SONG_LIST_URL);
            JSONObject json = JSONObject.parseObject(data);
            JSONArray list = json.getJSONArray("list");
//            JSONArray list = json.getJSONArray("list");
            for(Object obj:list){
                JSONObject resultData = (JSONObject) obj;
                AccompanyBean accompanyBean = JSONObject.parseObject(resultData.toJSONString(),AccompanyBean.class);
                System.out.println("accompanyBean.getId() = " + accompanyBean.getId());
                boolean flag = saveData(client,accompanyBean);
                if(!flag){
                    System.out.println("accompanyBean = " + accompanyBean.toString());
                }
            }
//            System.out.println("data = " + data);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            client.destroy();
        }
    }

    private static boolean saveData(MysqlClient client ,AccompanyBean ab){
        String sql = "INSERT INTO SONG_CCB (id, singer_name, operas_name, accompany_name, cid, accompanytime_length, songstime_length, accompany_size, \n" +
                "                      accompany_url, songs_url, accompanyimg_url, song_lyric_url) \n" +
                "    VALUES (?,?,?,?,?,?,?,?,?,?,?,?);";
        boolean flag = false;
        try {
            flag = client.execute(sql,ab.getId(),ab.getSingerName(),ab.getOperasName(),ab.getAccompanyName(),ab.getCid(),ab.getAccompanyTimeLength(),
                ab.getSongsTimeLength(),ab.getAccompanySize(),ab.getAccompanyUrl(),ab.getSongsUrl(),ab.getAccompanyUrl(),ab.getSongLyricUrl());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static void main(String[] args) {
        save();
    }
}
